<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span
          class="iconfont iconnew new"
          @click="$router.push({ name: 'index' })"
        ></span>
      </div>
      <span @click="followUserById" :class="{ active: post.has_follow }">{{
        post.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <div class="detail">
      <div class="title">{{ post.title }}</div>
      <div class="desc">
        <span>{{ post.user.nickname }}</span> &nbsp;&nbsp;
        <span>2019-9-9</span>
      </div>
      <div class="content" v-html="post.content" v-if="post.type == 1"></div>
      <!-- 1、controls：是否显示播放控制面板，如果没有这个面板，那么播放器不可见 
            2、src：播放的视频路径
            3、loop：循环播放
            4、autoplay：自动播放
            5、poster：封面，视频的第一帧画面
      -->
      <video
        v-else
        :src="post.content"
        controls
        poster="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Ff%2Fd3%2F350c73c884.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg"
      ></video>
      <div class="opt">
        <span class="like" :class="{ active: post.has_like }">
          <van-icon name="good-job-o" @click="likeThisPostById" />{{
            post.like_length ? post.like_length : "点赞"
          }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="@/assets/logo.png" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <!-- 底部评论区 -->
    <hmCommentFooter :post="post"></hmCommentFooter>
  </div>
</template>

<script>
import hmCommentFooter from "@/components/hmCommentFooter.vue";

import { getPostDetail, likeThisPost } from "@/apis/post.js";
import { unFollowUser, followUser } from "@/apis/user.js";
export default {
  data() {
    return {
      post: {
        user: {},
      },
    };
  },
  components: {
    hmCommentFooter,
  },
  async mounted() {
    // 获取文章动态id
    let id = this.$route.params.id;
    let res = await getPostDetail(id);
    //动态渲染 -- 重新渲染页面
    this.post = res.data.data;
  },
  methods: {
    //   1、关注户和取消关注
    async followUserById() {
      //   let res = await followUser(id);
      let res;
      let id = this.post.user.id;
      //   根据当前用户关注状态决定关注用户还是取消关注用户
      if (this.post.has_follow) {
        //已经关注了，点击取关
        res = await unFollowUser(id);
      } else {
        //   未关注，需要关注
        res = await followUser(id);
      }
      //   根据当前用户关注状态切换关注按钮样式变化
      this.post.has_follow = !this.post.has_follow;
      //   提示
      this.$toast.success(res.data.message);
    },
    //2、点赞和取消点赞
    async likeThisPostById() {
      let res = await likeThisPost(this.post.id);
      console.log(res);
      if (res.data.message == "点赞成功") {
        //为true：代表本次应该是取消
        //   点赞成功，点赞数加一
        this.post.like_length++;
      } else {
        //   点赞数减一
        this.post.like_length--;
      }
      //   根据点赞状态切换样式
      this.post.has_like = !this.post.has_like;
      //   提示
      this.$toast.success(res.data.message);
    },
  },
};
</script>

<style lang="less" scoped>
.articaldetail {
  padding-bottom: 60px;
}
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    &.active {
      background-color: transparent;
      color: #000;
      border: 1px solid #666;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    // text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img {
      width: 100%;
    }
    /deep/ p {
      text-indent: 2em;
    }
  }
  video {
    width: 100%;
    margin-bottom: 10px;
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .active {
    color: red;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>